#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<iostream>
using namespace std;

typedef long long LL;
const int N=510;
LL a[N],b[N],s[N];
int f[N][N],pre[N][N];

int main()
{
    int n,m;
    scanf("%d",&n);
    for (int i=1;i<=n;i++) scanf("%lld",&a[i]);
    scanf("%d",&m);
    for (int i=1;i<=m;i++) scanf("%lld",&b[i]);
    memset(f,0,sizeof(f));
    memset(pre,0,sizeof(pre));
    for (int i=1;i<=n;i++)
    {
     int p=0;
     for (int j=1;j<=m;j++)
     {
       f[i][j]=f[i-1][j];
       if (a[i]==b[j]) f[i][j]=f[i][p]+1,pre[i][j]=p;
       else if (a[i]>b[j] && f[i-1][j]>f[i-1][p]) p=j;
     }
    }
    int t=1,cnt=0;
    for (int j=2;j<=m;j++)
      if (f[n][j]>f[n][t]) t=j;
    printf("%d\n",f[n][t]);
    int x=n,y=t;
    while (f[x][y])
    {
      while(a[x]!=b[y]&&x) x--;
      s[++cnt]=b[y];
      y=pre[x][y];
    }
    for (int i=cnt;i>=1;i--) printf("%lld ",s[i]);
    return 0;
}